
#include "main.h"
#include "string.h"


bit PREFIX_EDIT = 0;

 

/* Prefix service */
 
/* Load Prefix from EEPROM */
void PrefixLoad_Index( unsigned int Index, PREFIX* Prefix )
{
	//unsigned int i, Address = E2_ADDR_PREFIX_START_ADDRESS + (Index * PREFIX_SIZE);
	unsigned int Address = EX_ADDR_START_PREFIXES + (Index * PREFIX_SIZE);

	Prefix -> Field.PrefixName = FREE_ENTRY; /* init. Prefix Name */
	
	Ex_LoadFrom(Prefix -> Data, Address, PREFIX_SIZE);

/* 	for ( i = 0; i < PREFIX_SIZE; i++ ) */
/* 	{ */
/* 		Prefix -> Data[i] = e2_Read(Address +i); */
/* 	} */
}

int PrefixLoad_Name( unsigned long Name, PREFIX* Prefix )
{
	unsigned int Index;

	if ( Name == FREE_ENTRY )
		return NAME_WAS_NOT_FOUND;

	for ( Index = 0; Index < PREFIX_MAXIMUM; Index++ )
	{
		PrefixLoad_Index(Index, Prefix);

		if ( Prefix -> Field.PrefixName == Name )
		{
			return Index;
		}		
	}

	return NAME_WAS_NOT_FOUND;
}


void PrefixClearAll ( void )
{
/* 	unsigned int i; */

/* 	/\* The first 2 prefixes are Default Call and SMS -> E2_ADDR_PREFIX_START_ADDRESS+ 2*PREFIX_SIZE*\/ */
/* 	for ( i = E2_ADDR_PREFIX_START_ADDRESS+ 2*PREFIX_SIZE; i < (  E2_ADDR_PREFIX_START_ADDRESS + (PREFIX_MAXIMUM * PREFIX_SIZE) );  i++ ) */
/* 	{ */
/* 		e2_Write( i,  0xFF ); */
/* 	} */

	Ex_MemSet ( (EX_ADDR_START_PREFIXES + 2*PREFIX_SIZE), 0xFF, (PREFIX_MAXIMUM * PREFIX_SIZE) );
	
}

int PrefixGetIndex( unsigned long Name )
{
	unsigned int Index;
	PREFIX Prefix;

	for ( Index = 0; Index < PREFIX_MAXIMUM; Index++ )
	{
		PrefixLoad_Index(Index, &Prefix);

		if ( Prefix.Field.PrefixName == Name )
		{
			return Index;
		}		
	}

	return NAME_WAS_NOT_FOUND; 
}

int PrefixFindFreeEntry( void )
{	
	unsigned int Index;
	PREFIX Prefix;

	for ( Index = 0; Index < PREFIX_MAXIMUM; Index++ )
	{
		PrefixLoad_Index(Index, &Prefix);
		if ( Prefix.Field.PrefixName == FREE_ENTRY )
		{
			return Index;
		}

/* 		if ( e2_Read( E2_ADDR_PREFIX_START_ADDRESS + (Index * PREFIX_SIZE) ) == 0xFF &&  */
/* 			  e2_Read( E2_ADDR_PREFIX_START_ADDRESS + (Index * PREFIX_SIZE) + 1 ) == 0xFF)			   */
/* 			return Index;  */
	}
	
	return NO_FREE_ENTRY; 					  /* The free place for Prefix was not found */
}
 
unsigned long PrefixGetName( unsigned int Index )
{
	PREFIX Prefix;
	PrefixLoad_Index(Index, &Prefix);
	
	return Prefix.Field.PrefixName; 
}

int PrefixFind( unsigned long PrefixName, PREFIX* Prefix )
{
	unsigned int Index;
	//unsigned long PrefixNameTemp = PrefixName;
	
  FIND_PREFIX:

	Index = PrefixLoad_Name ( PrefixName, Prefix );
	
	if ( Index == NAME_WAS_NOT_FOUND )
	{
		PrefixName >>= 4;
		PrefixName |= 0xF0000000;
		if ( PrefixName == FREE_ENTRY )
		{
			return PrefixLoad_Name ( DEFAULT_CALL_PREFIX, Prefix );
		}
	}
	else
	{
		return Index; 
	}
	
	goto FIND_PREFIX;

}
 
int PrefixWrite( unsigned int Index, PREFIX* Prefix )
{
	//unsigned int i, Address = E2_ADDR_PREFIX_START_ADDRESS + (Index * PREFIX_SIZE);
	unsigned int Address = EX_ADDR_START_PREFIXES + (Index * PREFIX_SIZE);

	if ( PREFIX_EDIT == TRUE || PrefixGetIndex ( Prefix -> Field.PrefixName ) == NAME_WAS_NOT_FOUND )
	{
		
		if ( Ex_SaveAndCheck(Address, Prefix -> Data, PREFIX_SIZE) == FALSE )
			return FALSE;

/* 		for ( i = 0; i < PREFIX_SIZE; i++ ) */
/* 		{	 */
/* 			e2_Write( (Address +i ),  Prefix -> Data[i] ); */
/* 		} */
		return TRUE; 
	}  

	return PREFIX_EXISTS; 
}
 
int PrefixRecord( PREFIX* Prefix )
{
	int Address, Index = PrefixGetIndex( Prefix -> Field.PrefixName ); 
	 

	if ( Index == NAME_WAS_NOT_FOUND )
	{
		Index = PrefixFindFreeEntry(); 

		if ( Index == NO_FREE_ENTRY )
		{
			return FALSE;
		}
		
		PrefixListView.ItemsCount++;
		//e2_Write(E2_ADDR_PREFIX_ITEMS_COUNT, PrefixListView.ItemsCount ); 
		Ex_WriteByte(EX_ADDR_PREFIX_ITEMS_COUNT, PrefixListView.ItemsCount ); 

		if ( PrefixListView.DisplayStringCount < 3 )
		{
			PrefixListView.DisplayStringCount++;
			//e2_Write( E2_ADDR_PREFIX_DISPLAY_STRINGS_COUNT, PrefixListView.DisplayStringCount );
			Ex_WriteByte( EX_ADDR_PREFIX_DISPLAY_STRINGS_COUNT, PrefixListView.DisplayStringCount );
		}

	}

	Address = EX_ADDR_START_PREFIXES + (Index * PREFIX_SIZE);
	//E2_SaveTo ( Address, Prefix -> Data, PREFIX_SIZE );
	
	return 	Ex_SaveTo( Address, Prefix -> Data, PREFIX_SIZE );
}
 
void PrefixDelete( unsigned long PrefixName )
{
	//unsigned int Index, i, Address;
	unsigned int Index, Address;
	
	Index = PrefixGetIndex ( PrefixName );
	
	if ( Index == NAME_WAS_NOT_FOUND )
		return;
	
	Address = EX_ADDR_START_PREFIXES + (Index * PREFIX_SIZE);

	Ex_MemSet(Address, 0xFF, PREFIX_SIZE); 

/* 	for ( i = 0; i < PREFIX_SIZE; i++ ) */
/* 	{	 */
/* 		e2_Write( (Address +i ),  0xFF ); */
/* 	} */

}
 
/* Find next from StartIndex up to Index == 0 
Input: Prefix Index (number); Output: return - PrefixName of first Index of not free entry*/
unsigned long Find_Up( unsigned char  IndexUp)
{
	unsigned long PrefixName;
	unsigned char StartIndex = IndexUp;
	
	while ( TRUE )
	{
		PrefixName = PrefixGetName( StartIndex ); 
		
		if ( PrefixName != FREE_ENTRY )
			return PrefixName;
		
		if ( StartIndex )
				StartIndex--;
			else
				StartIndex = PREFIX_MAXIMUM -1;
		
		if ( StartIndex == IndexUp ) 
			break;
	}
	
	return FREE_ENTRY;
}

/* Return PrefixName*/
unsigned long Find_Down( unsigned char IndexDown )
{
	unsigned long PrefixName;
	unsigned char StartIndex = IndexDown;

	if ( StartIndex == PREFIX_MAXIMUM )
		StartIndex = 0;

	while ( TRUE )
	{
		PrefixName = PrefixGetName( StartIndex ); 
		if ( PrefixName != FREE_ENTRY )
		{
			return PrefixName;
		}
		
		if ( ++StartIndex == PREFIX_MAXIMUM )
				StartIndex = 0;

		if ( StartIndex == IndexDown ) 
					break;
	}
	
	return FREE_ENTRY; 
}
